/**
 * 
 */
package com.projectsquirrel;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Arthi
 *
 */
public class photoUpload extends Activity{
	
	private static final int SELECT_PICTURE = 1;
	private static final int VIEW_PICTURE = 2;
	
	private String selectedImagePath;
    private String filemanagerstring;
    static TextView photoPath;

	
	 protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.photoupload);

	        //Get data captured from newObservation.java
	        //newObs = newObservation.getResults();
	        
	        // capture our View elements
	        final ImageButton settingsIconButton = (ImageButton) findViewById(R.id.settingnfoScreenIcon);
	        final ImageButton infoIconButton = (ImageButton) findViewById(R.id.infoScreenIcon);
	        final Button browsePhoto = (Button) findViewById(R.id.photoBrowseButton);
	        photoPath = (TextView) findViewById(R.id.photoPath);
	        final Button submitButton = (Button) findViewById(R.id.photoSubmitButton);
	         //final Button sharedPhotosButton = (Button) findViewById(R.id.sharedPhotosButton);
	        
	        
	        settingsIconButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent i = new Intent(photoUpload.this, settingsMenu.class);
			        startActivity(i);
				}	    	
		    });
	        
		    infoIconButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					Intent i = new Intent(photoUpload.this, infoMenu.class);
			        startActivity(i);
				}	    	
		    });
		    
		    browsePhoto.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					// select a file
	                Intent intent = new Intent();
	                intent.setType("image/*");
	                intent.setAction(Intent.ACTION_GET_CONTENT);
	                startActivityForResult(Intent.createChooser(intent,
	                        "Select Picture"), SELECT_PICTURE);
	                
				}	    	
		    });
		    
		    
		    
		    submitButton.setOnClickListener(new OnClickListener() {
				public void onClick(View v) {
					
					if(photoPath.getText().equals("-select photo-"))
						Toast.makeText(getApplicationContext(),"Please select a photo or click to return to main menu!",Toast.LENGTH_LONG).show();
					else{
						Toast.makeText(getApplicationContext(),"Photo Submitted!!",Toast.LENGTH_LONG).show();
						finish();
					}
				}	    	
		    });
		    
//		    sharedPhotosButton.setOnClickListener(new OnClickListener() {
//				public void onClick(View v) {
//					// select a file
//	                Intent intent = new Intent();
//	                intent.setType("image/*");
//	                intent.setAction(Intent.ACTION_GET_CONTENT);
//	                startActivityForResult(Intent.createChooser(intent,
//	                        "Select Picture"), VIEW_PICTURE);
//				}	    	
//		    });
		    
	 }
		    @Override
		    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		        super.onActivityResult(requestCode, resultCode, data);
		        
		        if (resultCode == RESULT_OK) {
		            if (requestCode == SELECT_PICTURE) {
		                Uri selectedImageUri = data.getData();

		                //OI FILE Manager
		                filemanagerstring = selectedImageUri.getPath();

		                //MEDIA GALLERY
		                selectedImagePath = getPath(selectedImageUri);
		                photoPath.setText(selectedImagePath.toString()); //Displaying the path using textview
		            }
		        }
		    }

	public String getPath(Uri uri) {
		        String[] projection = { MediaStore.Images.Media.DATA };
		        Cursor cursor = managedQuery(uri, projection, null, null, null);
		        if(cursor!=null)
		        {
		            //HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
		            //THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
		            int column_index = cursor
		            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		            cursor.moveToFirst();
		            return cursor.getString(column_index);
		        }
		        else return null;
		    }
	 
}
